package com.hwh.traffic.utils;




import android.os.Message;
import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;

import com.hwh.traffic.fragment.MyFragment;

import java.io.*;
import java.net.*;
import java.text.DecimalFormat;
import java.util.zip.CheckedOutputStream;

/**
 * 发送请求得到数据信息的接口类
 */
public class HttpUtil {
    private static BufferedOutputStream fos;

    //private static HttpURLConnection conn;
    //private static BufferedReader bfr;


    public static String httpPost(String url, String canshu) throws InterruptedException {
        try {

            HttpURLConnection conn = null;
            BufferedReader bfr = null;
//            CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
            URL ur = new URL(url);
            conn = (HttpURLConnection) ur.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            //conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            //conn.setInstanceFollowRedirects(true);
            conn.setConnectTimeout(10 * 1000);
            conn.setReadTimeout(10 * 1000);
            conn.setDoInput(true);
            conn.connect();
            DataOutputStream osw = new DataOutputStream(conn.getOutputStream());
            StringBuilder sb = new StringBuilder(canshu);
            System.out.println(sb.toString());
            osw.write(sb.toString().getBytes());
            osw.flush();
            osw.close();
            //Thread.sleep(2000);
            InputStream i = conn.getInputStream();
            bfr = new BufferedReader(new InputStreamReader(i));
            String line = "";
            String data = "";
            while ((line = bfr.readLine()) != null) {
                data = data + line;
            }
            bfr.close();
            return data;
        } catch (IOException e) {
            e.printStackTrace();
            return null;

        }

    }


    public static String httpGet(String url) {
        try {
            HttpURLConnection conn = null;
            BufferedReader bfr = null;

            URL ur = new URL(url);
            conn = (HttpURLConnection) ur.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            conn.setInstanceFollowRedirects(true);
            conn.setConnectTimeout(10 * 1000);
            conn.setReadTimeout(10 * 1000);
            conn.setDoInput(true);
            conn.connect();
            //读取返回数据
            Thread.sleep(20);
            InputStream i = conn.getInputStream();
            bfr = new BufferedReader(new InputStreamReader(i));
            String line;
            String data = "";
            while ((line = bfr.readLine()) != null) {
                data = data + line;
            }
            bfr.close();
            conn.disconnect();
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void download(String url, FileOutputStream fos, MyFragment.MyHandler myHandler){

        try {
            HttpURLConnection conn = null;
            BufferedReader bfr = null;

            URL ur = new URL(url);
            conn = (HttpURLConnection) ur.openConnection();
            conn.setRequestMethod("GET");
            //conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            conn.setInstanceFollowRedirects(true);
            conn.setConnectTimeout(10 * 1000);
            conn.setReadTimeout(10 * 1000);
            conn.setDoInput(true);
            conn.connect();
            //读取返回数据
            Thread.sleep(20);
            InputStream i = conn.getInputStream();
            bfr = new BufferedReader(new InputStreamReader(i));
            byte buffer[] = new byte[4096];
            int readsize = 0;
            String line;
            String data = "";
            int downloadsize = 0;
            int total = conn.getContentLength();
            System.out.println("流大小 " + conn.getContentLength());
            while ((readsize = i.read(buffer)) > 0) {

                fos.write(buffer, 0, readsize);
                downloadsize = downloadsize+readsize;

                float num= (float)downloadsize * 100/total;
                DecimalFormat df = new DecimalFormat("0");//格式化小数
                String s = df.format(num);//返回的是String类型
                fos.flush();
                Message msg = new Message();
                msg.what = Integer.parseInt(s);
                myHandler.sendMessage(msg);
            }
            System.out.println("下载完成");
            fos.close();
            conn.disconnect();

        } catch (Exception e) {
            e.printStackTrace();

        }
    }



}
